﻿/**
* ...
* @author Default
* @version 0.1
*/

package com.crlclr.managers {
	import com.crlclr.*;
	import com.crlclr.common.*;
	import com.crlclr.ui.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.system.*;
	

	public class SkinManager extends EventDispatcher{
		public static const SKININIT:String = "skinInit";
		
		public var _path:String;
		public var loader:Loader;
		public function SkinManager() {
			if (instance != null) throw new Error("can't create instance");
			loader = new Loader();
		}
		
		private static var instance:SkinManager = new SkinManager();
		public static function getInstance():SkinManager {
			return instance;
		}
		public function get path():String {
			return this._path;
		}
		public function load(path:String):void {
			this._path = path;
			var loadercontex:LoaderContext = new LoaderContext();
			loadercontex.applicationDomain = ApplicationDomain.currentDomain;
			
			var adpater:LoaderAdapter = new LoaderAdapter(this.loader, this._path);
			LoaderManager.getInstance().addItem(adpater);			
		}
		private function bindEvent():void {
			loader.addEventListener(Event.INIT,initHander);
		}
		public function removeEvent():void {
			loader.removeEventListener(Event.INIT,initHander)
		}
		private function initHander(event:Event):void {
			removeEvent();
			dispatchEvent(new Event(SkinManager.SKININIT));
		}
		public function getClass(className:String):Class {
			try {
				return loader.contentLoaderInfo.applicationDomain.getDefinition(className)  as  Class;
			} catch (e:Error) {
				throw new Error(className + " definition not found in "+e.toString() );
			}
			return null;
		}
		
	}
	
	
}
